package com.hp.gxw.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.hp.gxw.cache.CommonCache;
import com.hp.gxw.constant.BaseEntry;
import com.hp.gxw.constant.LayTableR;
import com.hp.gxw.entity.BaseCommonDataDictionaryAndSet;
import com.hp.gxw.entity.Process;
import com.hp.gxw.service.IProcessService;
import com.hp.gxw.utils.ConditionUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author huangping
 * @since 2024-01-04
 */
@RestController
@RequestMapping("/manage/process")
public class ProcessController extends CommonController{

    @Autowired
    public CommonCache commonCache;

    @Autowired
    private IProcessService processService;

    @PostMapping("/list")
    public LayTableR list(Integer page, Integer limit, String master) throws Exception {

        QueryWrapper<Object> ew = ConditionUtil.convert(master, new QueryWrapper<>());
        ew.eq("type", "加工");
        BaseEntry baseEntry = commonCache.init("process");
        IService<Object> iService = baseEntry.getIService();
        Page<Object> pageList = iService.page(new Page<>(page, limit), ew);
        List<BaseCommonDataDictionaryAndSet> collect = baseEntry.getSetList().stream().filter(e -> e.getQueryConstantId() != null).collect(Collectors.toList());
        List<Map> resultList = dataTransfer(pageList.getRecords(), collect);
        return LayTableR.result(0, pageList.getTotal(), resultList);
    }

    @PostMapping("/listType")
    public LayTableR listType(Integer page, Integer limit, String key) {
        QueryWrapper<Process> ew = new QueryWrapper<>();
        ew.eq("type", "订单");
        if (StringUtils.isNotEmpty(key)){
            ew.like("name", key);
        }
        Page<Process> processPage = processService.page(new Page<>(page, limit), ew);
        return LayTableR.result(0, processPage.getTotal(), processPage.getRecords());
    }

    @PostMapping("/processType")
    public LayTableR processType(String type) {
        if (StringUtils.isEmpty(type)){
            return LayTableR.result(1, 0, null);
        }
        QueryWrapper<Process> ew = new QueryWrapper<>();
        ew.eq("type", type);
        List<Process> list = processService.list(ew);
        return LayTableR.result(0, list.size(), list);
    }
}